//
//  OpBaseTabarViewController.m
//  LoveFreshBeeReplica
//
//  Created by skywalker on 16/12/30.
//  Copyright © 2016年 斯芬克斯. All rights reserved.
//

#import "OpBaseTabarViewController.h"
#import "OpMineController.h"
#import "OpHeaderPageController.h"
#import "OpShoppingCarController.h"
#import "OpFlashDeliverMarketController.h"
#import "OpBaseNavigationController.h"
#import "OpBaseTabBarButton.h"
#define kTabBarHeight 49
@interface OpBaseTabarViewController ()
//自定义Tabar
@property(nonatomic,strong)UIView * tabarView;
///前一次选中的按钮
@property (nonatomic, strong)OpBaseTabBarButton *selectLastBtn;
@end
static OpBaseTabarViewController * controller =nil;
@implementation OpBaseTabarViewController

#pragma mark-创建TabarController
+(OpBaseTabarViewController *)sharedController{
    @synchronized (controller) {
        if (controller == nil) {
            controller=[[self alloc]init];
            NSArray * titles=@[@"首页",@"闪送超市",@"购物车",@"我的"];
            NSArray * vcs=@[[[OpHeaderPageController alloc]init],[[OpFlashDeliverMarketController alloc]init],[[OpShoppingCarController alloc]init],[[OpMineController alloc]init]];
            NSArray *images = @[@"v2_home",@"v2_order",@"shopCart",@"v2_my"];
            NSArray *selectedImages = @[@"v2_home_r",@"v2_order_r",@"shopCart_r",@"v2_my_r"];

            [controller addChildViewController:vcs andTitles:titles andImages:images andSelectedImages:selectedImages andTaBarNaviChildVC:[[OpBaseNavigationController alloc]init]];
        }
    }
    return controller;
    
}
- (void)buildTabBar{
    self.tabBar.hidden = YES;
    CGFloat tabBarViewY = self.view.frame.size.height - 49;
    _tabarView = [[UIView alloc]initWithFrame:CGRectMake(0, tabBarViewY, kScreenHeight, 49)];
    _tabarView.userInteractionEnabled = YES;
    _tabarView.backgroundColor = [UIColor whiteColor];
    _tabarView.alpha = 0.9;
    [self.view addSubview:_tabarView];
}
+(instancetype)allocWithZone:(NSZone *)zone{
    @synchronized(self){
        if (controller == nil) {
            controller = [super allocWithZone:zone];
            return  controller;
        }
    }
    return nil;
}
-(void)addChildViewController:(NSArray *)childsVC andTitles:(NSArray *)titles andImages:(NSArray *)images andSelectedImages:(NSArray *)SelectedImages andTaBarNaviChildVC:(OpBaseNavigationController *)tabBarNaviChildVC{
    for (int i= 0; i<childsVC.count; i++) {
        UINavigationController *tabBarNaviChildVC = [[UINavigationController alloc]
                                                     initWithRootViewController:childsVC[i]];
        
        [self addChildViewController:tabBarNaviChildVC];
        
        [self creatButtonWithNormalName:images[i]
                          andSelectName:SelectedImages[i]
                               andTitle:titles[i] andTag:i
                               andIndex:childsVC.count];
    }
  OpBaseTabBarButton * button = self.tabarView.subviews[0];
    [self changeViewController:button];
}

#pragma mark -创建button
- (void)creatButtonWithNormalName:(NSString *)normal andSelectName:(NSString *)selected andTitle:(NSString *)title andTag:(int)tag andIndex:(NSInteger)index{
    OpBaseTabBarButton * customButton=[OpBaseTabBarButton buttonWithType:UIButtonTypeCustom];
    customButton.tag=tag;
    customButton.imageView.contentMode = UIViewContentModeCenter;
    [customButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [customButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    
    [customButton setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
    [customButton setImage:[UIImage imageNamed:selected] forState:UIControlStateDisabled];
    [customButton setTitle:title forState:UIControlStateNormal];
    [customButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [customButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
    [customButton addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchDown];
    
    ///设置大小
    NSInteger count=index;
    CGFloat buttonW=_tabarView.frame.size.width /count;
    CGFloat buttonH=_tabarView.frame.size.height;
    customButton.frame=CGRectMake(buttonW * tag, 0, buttonW, buttonH);
   [_tabarView addSubview:customButton];

}

-(void)changeViewController:(OpBaseTabBarButton *)button{
    button.enabled = NO;
    if (_selectLastBtn != button) {
        _selectLastBtn.enabled = YES;
    }
    _selectLastBtn = button;
    self.selectedViewController = self.viewControllers[button.tag];
    
}
///是否隐藏TaBar
-(void)hidesTabar:(BOOL)isHides andAnimated:(BOOL)animated{
    self.isTabBarHidden = isHides;
    if (isHides== YES)
    {
        if (self.tabarView.frame.origin.y == self.view.frame.size.height)
        {
            return;
        }
    }
    else
    {
        if (_tabarView.frame.origin.y == self.view.frame.size.height - kTabBarHeight)
        {
            return;
        }
    }
    if (animated == YES)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3f];
        if (isHides == YES)
        {
            _tabarView.frame = CGRectMake(_tabarView.frame.origin.x, _tabarView.frame.origin.y + kTabBarHeight, _tabarView.frame.size.width, _tabarView.frame.size.height);
        }
        else
        {
            _tabarView.frame = CGRectMake(_tabarView.frame.origin.x, _tabarView.frame.origin.y - kTabBarHeight, _tabarView.frame.size.width, _tabarView.frame.size.height);
        }
        [UIView commitAnimations];
    }
    else
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3f];
        if (isHides == YES)
        {
            _tabarView.frame = CGRectMake(_tabarView.frame.origin.x, _tabarView.frame.origin.y + kTabBarHeight, _tabarView.frame.size.width, _tabarView.frame.size.height);
        }
        else
        {
            _tabarView.frame = CGRectMake(_tabarView.frame.origin.x, _tabarView.frame.origin.y - kTabBarHeight, _tabarView.frame.size.width, _tabarView.frame.size.height);
        }
        [UIView commitAnimations];
    }
    
    
}





@end
